<?php

namespace App\Services;

use App\Models\ExamPaperQuestion;
use App\Models\ExamQuestionOption;
use App\Models\UserQuestionRecord;

class CertificationExaminationService extends  BaseService
{
    public function certificationExaminationInfo($exam_paper_id){

        // 单选题数量
        $singleChoiceCount = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)->where('question_type', 0)->count();
        // 多选题数量
        $multipleChoiceCount = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)->where('question_type', 1)->count();
        // 判断题数量
        $estimateCount = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)->where('question_type', 2)->count();
        // 填空题数量
        $fillBlankCount = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)->where('question_type', 3)->count();
        // 简答题数量
        $shortAnswerCount = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)->where('question_type', 4)->count();
        // 计算总试题数量
        $totalQuestions = $singleChoiceCount + $multipleChoiceCount + $estimateCount +$fillBlankCount+$shortAnswerCount;
        return [
            'singleChoiceCount' => $singleChoiceCount,
            'multipleChoiceCount' => $multipleChoiceCount,
            'estimateCount' => $estimateCount,
            'fillBlankCount' => $fillBlankCount,
            'shortAnswerCount' => $shortAnswerCount,
            'totalQuestions' => $totalQuestions,
        ];
    }
    /**
     * 获取试卷下试题列表
     * @return
     */
    public function paperQuestion($users_id,$certificate_id,$exam_paper_id) {
        // 查询用户的答题记录
        $userAnswers = UserQuestionRecord::where('exam_paper_id', $exam_paper_id)
            ->where('certificate_id', $certificate_id)
            ->where('users_id', $users_id)->get();

        // 试题
        $exam = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)
            ->select('id','question_stem','question_type','order','score','is_required','is_displayed')
            ->orderBy('order','asc')
            ->get()->toArray();
        // 提取试题的ID
        $examQuestionIds = array_column($exam, 'id');
        // 查询选项
        $options = ExamQuestionOption::whereIn('exam_paper_question_id', $examQuestionIds)
            ->select('id','exam_paper_question_id','option_content','is_correct','order')
            ->orderBy('order','asc')->get();

        $examWithOptions = [];
        // 遍历试题
        foreach ($exam as $k=>$v) {
            $exam[$k]['options'] = [];
            $examWithOptions[$v['id']] = $exam[$k];
        }
        // 遍历选项，将选项与试题关联
        $options = $options->toArray();
        foreach ($options as $option) {
            if (isset($examWithOptions[$option['exam_paper_question_id']])) {
                $examWithOptions[$option['exam_paper_question_id']]['options'][] = $option;
            }
        }

        $response = [
            'questions' => $examWithOptions,
            'userAnswers' => $userAnswers,
        ];
        return $response;
    }
    /**
     * 答题进度
     * @return
     */
    public function answerProgress($users_id,$exam_paper_id) {
        // 查询用户的答题记录
        $userAnswers = UserQuestionRecord::where('exam_paper_id', $exam_paper_id)->where('users_id', $users_id)->get();
        $examPaperQuestion = ExamPaperQuestion::where('exam_paper_id', $exam_paper_id)->orderBy('order','asc')->get();
        $questionArr = [];
        foreach ($examPaperQuestion as $v){
            $questionArr[$v->id] = 0;
        }
        foreach ($userAnswers as $v){
            if(isset($questionArr[$v->exam_paper_question_id])){
                $questionArr[$v->exam_paper_question_id] = 1;
            }
        }
        // 查询用户答对的题目数量
        $correctAnswersCount = UserQuestionRecord::where('exam_paper_id', $exam_paper_id)
            ->where('users_id', $users_id)
            ->where('is_correct', 1)
            ->count();

        // 查询用户答错的题目数量
        $wrongAnswersCount = UserQuestionRecord::where('exam_paper_id', $exam_paper_id)
            ->where('users_id', $users_id)
            ->where('is_correct', 0)
            ->count();
        // 查询总的题目数量
        $totalQuestionsCount = count($questionArr);
        // 计算答题完成率
        $completionRate = 0;
        if ($totalQuestionsCount > 0) {
            $completionRate = ($correctAnswersCount / $totalQuestionsCount) * 100;
            $completionRate = number_format($completionRate, 2);
        }
        // 计算答题进度
        $progress = ($correctAnswersCount+$wrongAnswersCount).'/'.$totalQuestionsCount;
        return [
            'questionArr' => $questionArr,
            'correctAnswersCount' => $correctAnswersCount,
            'wrongAnswersCount' => $wrongAnswersCount,
            'totalQuestionsCount' => $totalQuestionsCount,
            'completionRate' => $completionRate,
            'progress' => $progress,
        ];
    }
}
